package com.t.bricks.bussiness.db.dao.flowwork.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.flowwork.FlowWorkDao;
import com.t.bricks.bussiness.db.entity.flowwork.FlowWork;
import com.t.bricks.bussiness.db.mapper.flowwork.FlowWorkMapper;
import com.t.bricks.bussiness.db.mapper.flowwork.FlowWorkNodeMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class FlowWorkDaoImpl implements FlowWorkDao<FlowWork> {

	@Autowired
	private FlowWorkMapper flowWorkMapper;
	
	@Autowired
	private FlowWorkNodeMapper flowWorkNodeMapper;

	/**
	 * 创建流程实例
	 * @param flowWork
	 * @param sInitiateDescription 发起节点的描述
	 * @return MsgEmity
	 */
	@Override
	@Transactional
	public MsgEmity createWork(FlowWork flowWork, String sInitiateDescription) {
		try {
			int i = flowWorkMapper.addWork(flowWork);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		try {
			int i = flowWorkNodeMapper.createNode(
					flowWork.getuId(),
					flowWork.getsDefineSign(),
					flowWork.getsCreator());
			if (i < 1) {
				Log.error("新增节点未能得到预期影响值(预期大于0):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增节点数据失败！", 7003);
			}
			
//			FlowWorkNode flowWorkNode = flowWorkNodeMapper.findByType2(flowWork.getuId());
//			if (null == flowWorkNode) {
//				Log.error("没有找到对应的发起人节点:", flowWork.getuId());
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
//				return new MsgEmity(false, "没有找到对应的发起人节点,新增节点数据失败！", 7005);
//			}
			
		} catch (Exception e) {
			Log.error("新增节点发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "新增节点数据失败！", 7006);
		}
		
		return new MsgEmity(true, "新增成功！", flowWork);
	}

	/**
	 * 删除
	 * @Param id 记录编号
	 * @Param iVersion 数据版本号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity delWork(String uId, Integer iVersion) {
		MsgEmity me = this.del(uId, iVersion);
		if (!me.isSuccess()) {
			return me;
		}
		
		try {
			int i = flowWorkNodeMapper.delBysWorkId(uId);
			if (i < 1) {
				Log.error("删除未能得到预期影响值(预期大于0):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "删除数据失败！", 7102);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "删除失败！", 7103);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}

	/**
	 * 修改状态
	 * @param uId 编号
	 * @param iState 状态值
	 * @param iVersion 记录版本号
	 * @param sMemo 备注
	 * @return MsgEmity 返回执行情况
	 */
	@Override
	public MsgEmity changeState(String uId, Integer iState, Integer iVersion, String sMemo) {
		try {
			int i = flowWorkMapper.changeState(uId, iState, iVersion, sMemo);
			if (i < 1) {
				List<String> fieldNames = Arrays.asList("iState", "iVersion");
				MsgEmity me = this.getValueByFieldName(uId, fieldNames, false);//如果影响数小于1,则检查数据
				if (!me.isSuccess()) {
					return me;
				}
				
				Map<String, Object> map = me.getData();
				if (null == map) {
					return new MsgEmity(false, "没有找到对应数据！", 7001);//删除影响数失败,但数据也不存在则视为成功
				}
				
				if (iVersion != Integer.valueOf(map.get("iVersion").toString()).intValue()) {
					return new MsgEmity(false, "更新状态失败，系统中的数据可能已经被更新！", 7002);
				}
				
				if (iState >= 0 && iState < Integer.valueOf(map.get("iState").toString()).intValue()) {
					return new MsgEmity(false, "更新状态失败，状态禁止逆操作！", 7003);
				}

				Log.error("更新状态未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新状态数据失败！", 7004);
			}
		} catch (Exception e) {
			Log.error("更新状态时发生异常:", e.getMessage());
			return new MsgEmity(false, "更新状态失败！", 7005);
		}
		
		return new MsgEmity(true, "更新状态成功！", 7999);
	}

}
